using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Drawing.Design;
using System.Drawing;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
using System.Reflection;

using SCS.Web.Controls;

namespace SCS.Web.Controls.Design
{
	internal class MenuItemStyleCollectionConverter : TypeConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type t)
		{
			if (t == typeof(string))
				return true;

			return base.CanConvertFrom(context, t);
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destType) 
		{
			if (destType == typeof(InstanceDescriptor) || destType == typeof(string)) 
			{
				return true;
			}
			return base.CanConvertTo(context, destType);
		}	
		
		
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo info, object value)
		{
			if (value is string)
			{
				string itemStyleGroupList = (string)value;
				
				try
				{
					MenuItemStyleCollection itemStyleCollection = new MenuItemStyleCollection();

					string[] styleGroups = itemStyleGroupList.Split(info.TextInfo.ListSeparator[0]);

					foreach (string styleGroup in styleGroups)
					{
						string[] itemStyles = styleGroup.Split(' ');
						string[] args = new string[5];

						foreach (string itemStyle in itemStyles)
						{
							string name = itemStyle.Split('=')[0].Trim();
							string val = itemStyle.Split('=')[1].Trim();

							name = name.Trim();
							val = val.Trim();

							switch (name)
							{
								case "Over":
									args[1] = val;
									break;

								case "Selected":
									args[2] = val;
									break;

								case "SelectedOver":
									args[3] = val;
									break;

								case "Disabled":
									args[4] = val;
									break;

								default:
									args[0] = val;
									break;
							}
						}

						itemStyleCollection.Add(new MenuItemStyle(args[0], args[1], 
							args[2], args[3], args[4]));

					}
					return itemStyleCollection;
				}
				catch
				{
					return new MenuItemStyleCollection();
				}				
			}

			return base.ConvertFrom(context, info, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo info, object value, Type destType )
		{
			if (destType == typeof(string))
			{
				MenuItemStyleCollection itemStyleCollection = (MenuItemStyleCollection)value;
				string itemStyleList = string.Empty;
				string text = string.Empty;

				foreach (MenuItemStyle itemStyle in itemStyleCollection)
				{	
					text += info.TextInfo.ListSeparator[0];

					//text += string.Format("{0} {1}", 
					//	info.TextInfo.ListSeparator[0], itemStyle.Level);
					
					if (itemStyle.Normal.Length > 0)
						text += string.Format(" {1}", itemStyle.Normal);

					if (itemStyle.Over.Length > 0)
						text += string.Format(" Over={1}", itemStyle.Over);

					if (itemStyle.Selected.Length > 0)
						text += string.Format(" Selected={1}", itemStyle.Selected);

					if (itemStyle.SelectedOver.Length > 0)
						text += string.Format(" SelectedOver={1}", itemStyle.SelectedOver);

					if (itemStyle.Disabled.Length > 0)
						text += string.Format(" Disabled={1}", itemStyle.Disabled);					
				}

				if (text.Length > 0)
					text = text.Substring(info.TextInfo.ListSeparator[0].ToString().Length);
								
				return itemStyleList;
			}
			else if (destType == typeof(InstanceDescriptor)) 
			{
				// string constructor
				Type[] types = new Type[] { typeof(string) };
				ConstructorInfo constructorInfo = typeof(MenuItemStyleCollection).GetConstructor(types);
				
				MenuItemStyleCollection itemStyles = (MenuItemStyleCollection)value;
				
				string itemStyleList = string.Empty;

				foreach (MenuItemStyle itemStyle in itemStyles)
				{
					itemStyleList += string.Format(",{1} {2} {3} {4}", 
						itemStyle.Normal, itemStyle.Over, itemStyle.Selected, 
							itemStyle.SelectedOver, itemStyle.Disabled);
				}

				if (itemStyleList.Length > 0)
				{
					itemStyleList = itemStyleList.Substring(1);
				}

				object[] args = new object[] { itemStyleList };


				InstanceDescriptor descriptor = new InstanceDescriptor(constructorInfo, args, true);

				return descriptor;
			}

			return base.ConvertTo(context, info, value, destType);
		}
	}
}
